package com.xgq.drink.utils;

import com.xgq.drink.entity.OrderProduct;
import com.xgq.drink.entity.DiscountInfo;
import com.xgq.drink.service.IDiscountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component
public class OrderAmountCalculator {
    private final IDiscountInfoService discountInfoService;
    @Autowired
    public OrderAmountCalculator(IDiscountInfoService discountInfoService) {
        this.discountInfoService = discountInfoService;
    }

    /**
     * 计算订单的总金额、优惠金额和折扣后的金额
     *
     * @param orderProducts 订单中的商品列表
     * @return BigDecimal 总计金额
     */
    public static BigDecimal calculateOrderAmounts(List<OrderProduct> orderProducts) {
        BigDecimal numPrice = BigDecimal.valueOf(0.00);
        for (OrderProduct product : orderProducts) {
            numPrice=BigDecimalUtils.add(numPrice,product.getCurrentPrice());
            System.out.println("----");
            System.out.println(numPrice);
            System.out.println(product.getCurrentPrice());
        }
        return numPrice;
    }
    public static BigDecimal calculateOrderAmountsToFinal(List<OrderProduct> orderProducts) {
        BigDecimal numPrice = BigDecimal.valueOf(0.00);
        for (OrderProduct product : orderProducts) {
            numPrice=BigDecimalUtils.add(numPrice,product.getCurrentPrice());
        }
        return numPrice;
    }
    /**
     * 计算折扣后的金额
     *
     * @param originalAmount 原金额
     * @param discountInfo   优惠信息ID
     * @return 折扣后的金额
     */
    public BigDecimal calculateDiscountAmount(BigDecimal originalAmount, DiscountInfo discountInfo) {
        if (discountInfo==null)return originalAmount;
        switch (discountInfo.getDiscountStrategy()) {
            case 1: // 直扣
                return originalAmount.subtract(discountInfo.getDiscountValue());
            case 2: // 折扣率
                return originalAmount.multiply(discountInfo.getDiscountValue());
            default:
                return originalAmount; // 未知策略，返回原金额
        }
    }
}